package com.bj58.ecdata.dashboard.databll;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;

import com.alibaba.fastjson.JSONObject;
import com.bj58.ecdata.dashboard.cache.ServiceBeanFactory;
import com.bj58.ecdata.dashboard.dbentity.ZSZP_qushi;
import com.bj58.ecdata.dashboard.dbentity.ZhishuZhaopinFYB;
import com.bj58.ecdata.dashboard.service.AbstractBaseService;
import com.bj58.ecdata.dashboard.utils.DDateUtils;
import com.bj58.ecdata.dashboard.utils.DateUtil;
import com.bj58.ecdata.dashboard.utils.ReflectUtil;
import com.google.common.collect.Lists;

public class ZhishuZhaopinBLL {

	private static AbstractBaseService<ZhishuZhaopinFYB> zhaopinService =ServiceBeanFactory.get(ZhishuZhaopinFYB.class,null);
	private static AbstractBaseService<ZSZP_qushi> qushiService =ServiceBeanFactory.get(ZSZP_qushi.class,null);
	
	/**
	 * * @param cityName
     * * @param cateName
	 * * @param Months
	 * * @return
	 */
	
	public static Map<String,Object> getZhaopinJson(String cityName,String cateName,String Months){
		
		final String PATTEN_GENERAL = "belong_city1_name = '%s' AND belong_cate2_name = '%s' AND stat_date = '%s'";
		final String select = "list_name_belonglocal,avgsalary_json,zprs_json,increases_json";
		
		//转换成当前月份的第一天
		Date date = new Date();
		String dateStr = DDateUtils.dateToStr(date, "yyyy-MM-dd");
		int  times = Integer.parseInt(Months); 
	     //一个月内，对应昨天的数据
	  	if(times == 1){
	  		dateStr = DateUtil.getYestoday("yyyy-MM-dd");  		
	  	}else{
	  		//三个月内，对应三个月前的今天
	  		dateStr = DateUtil.getDayBeforeMonth(dateStr, times);
	  	}
	  	String condition = String.format(PATTEN_GENERAL, cityName,cateName,dateStr);
		List<ZhishuZhaopinFYB> list = zhaopinService.getListByConditon(select, condition, null);
		
		
		Map<String,Object> maps = new HashMap<String,Object>();	
		JSONArray array;
		for (ZhishuZhaopinFYB zhaopin : list) {	
			maps.put("city", zhaopin.getList_name_belonglocal());
			array = JSONArray.fromObject(zhaopin.getAvgsalary_json());
			maps.put("avg",array);
			array = JSONArray.fromObject(zhaopin.getZprs_json());
			maps.put("zprs", array);
			array = JSONArray.fromObject(zhaopin.getIncreases_json());
			maps.put("increases", array);
		}
		return maps;	
	}
	
	/**
	  * * @param city
	  * * @param cate
	  * * @return
	 */	
	private static final String PATTEN_GENERAL = "city1='%s' AND cate2='%s' group by stat_date" ;
	public static String getQushiDataBll(String city,String cate){
			
		String condition = String.format(PATTEN_GENERAL,city,cate);
		
		List<ZSZP_qushi> list = qushiService.getListByConditon(null, condition, "stat_date");
		
		JSONObject json = new JSONObject();

	    json.put("dataList", list);
		return json.toString();
	}
	/**
	 * 获取城市类别和cate类别
	 */
	public static Map<String,List<String>> getCityAndCate(){				
		//城市
		List<ZSZP_qushi> cityList = qushiService.getListByConditon("DISTINCT city1", null, null);
		List<String> cityList1 = Lists.newArrayList();
		for (ZSZP_qushi zszp_qushi : cityList) {
			String city = (String) ReflectUtil.getVal(zszp_qushi, "city1");
			cityList1.add(city);
		}		
	  
	    //cate类别
	    List<ZSZP_qushi> cateList = qushiService.getListByConditon("DISTINCT cate2", null, null);
	    List<String> cateList1 = Lists.newArrayList();
	    for (ZSZP_qushi zszp_qushi : cateList) {
			String cate = (String) ReflectUtil.getVal(zszp_qushi, "cate2");
			cateList1.add(cate);
		}
        Map<String,List<String>> map = new HashMap<String, List<String>>();
        map.put("cityList", cityList1);
        map.put("cateList", cateList1);
		return map;
	}
	
	/**
	 *  城市排行榜
	 */
	public static String getCityDataBll(Map<String,String> map){
		return null;
		
	}
	
	/***
	 * 职位排行榜
	 */
	public static String getPositionData(Map<String,String> map){
		return null;
		
	}
}
